//页面初始化
import { store } from './store'

export function InitPageGenerator(parentId, bgType, rgb, opa, img) {
  if (rgb == null) {
    rgb = '0xFFFFFF'
  }
  rgb = rgb.replace('#', '0x')
  img = img.replace('.png', '').replace('P', '')
  // let init_page = parentId + `= init_page(\r\n` + rgb + `,--rgb \r\n` + opa + ` --opa \r\n` + `)`
  if (bgType == '2' && img != '') {
    opa = 0
    rgb = '0xFFFFFF'
    let png =
      `parentId_back = create_png( \r\n` +
      parentId +
      `,` +
      img +
      `,--图片路径 \r\n` +
      `0` +
      `,--基准Id \r\n` +
      `9,0,0` +
      `--对齐方式,横纵坐标 \r\n` +
      `)`
    return (
      parentId + `= init_page(\r\n` + rgb + `,--rgb \r\n` + opa + ` --opa \r\n` + `)` + `\r\n` + png
    )
  } else {
    opa = 255
    return parentId + `= init_page(\r\n` + rgb + `,--rgb \r\n` + opa + ` --opa \r\n` + `)`
  }
}

export function ButtonGenerator(
  buttonId,
  parentId,
  width,
  height,
  color,
  colorLine,
  widthLine,
  opa,
  radius,
  baseId,
  align,
  X,
  Y,
  event1,
  event2,
  userId,
  bgType,
  pngId,
  pngAlign,
  isTitle,
  titleText,
  fontSize,
  fontColor,
  fontType,
  titleAlign,
  hidden
) {
  if (color == null) {
    color = '0x000000'
  }
  if (colorLine == null) {
    colorLine = '0xFFFFFF'
  }
  if (bgType == '2') {
    opa = 0
  }
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  baseId = 0
  let buttonCode =
    buttonId +
    `= create_button_ex (` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    color.replace('#', '0x') +
    `,` +
    colorLine.replace('#', '0x') +
    `,` +
    widthLine +
    `,` +
    opa +
    `,` +
    radius +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    X +
    `,` +
    Y +
    `,` +
    event1 +
    `,` +
    event2 +
    `,` +
    userId +
    `)`

  let bgCode = `\r\n`
  let titleCode = `\r\n`
  if (bgType == '2' && pngId != '') {
    bgCode =
      bgCode +
      `create_png(` +
      buttonId +
      `,\r\n` +
      pngId.replace('P', '').replace('.png', '') +
      `,--图片路径 \r\n` +
      `0` +
      `,--基准Id \r\n` +
      pngAlign +
      `,` +
      `0,0--对齐方式,横纵坐标 \r\n` +
      `)`
  }
  if (isTitle == '1') {
    if (titleAlign == '' || titleAlign == undefined || titleAlign == '') {
      titleAlign = 9
    }
    if (fontColor == null || fontSize == '' || fontSize == undefined) {
      fontColor = '0x000000'
    }
    titleCode =
      titleCode +
      `\r\n` +
      `create_label(` +
      buttonId +
      `,` +
      `"` +
      titleText +
      `", ` +
      (Math.round(fontSize / 2) - 5) +
      `,` +
      `--字体大小\r\n` +
      fontColor.replace('#', '0x') +
      `,` +
      `--字体颜色\r\n` +
      fontType +
      `,` +
      `--字体样式,0表示正常,1表示斜体,2表示粗体\r\n` +
      0 +
      `,` +
      titleAlign +
      `,` +
      0 +
      `,` +
      0 +
      `--对齐方式,横纵坐标\r\n` +
      `)`
  }
  let hiddenCode = `\r\nset_obj_hidden(` + buttonId + `,` + hidden + `)`
  return buttonCode + bgCode + titleCode + hiddenCode
}

/**
 * 普通静态文本显示组件
 * @param {*} parentId
 * @param {*} strLabel
 * @param {*} fontSize
 * @param {*} fontColor
 * @param {*} fontType
 * @param {*} baseId
 * @param {*} Align
 * @param {*} X
 * @param {*} Y
 * @returns
 */
export function LabelGenerator(
  labelId,
  parentId,
  strLabel,
  fontSize,
  fontColor,
  fontType,
  baseId,
  Align,
  X,
  Y,
  hidden
) {
  baseId = 0
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  return (
    labelId +
    `= create_label(` +
    parentId +
    `,` +
    `"` +
    strLabel +
    `", ` +
    (Math.round(fontSize / 2) - 5) +
    `,` +
    `--字体大小\r\n` +
    fontColor.replace('#', '0x') +
    `,` +
    `--字体颜色\r\n` +
    fontType +
    `,` +
    `--字体样式,0表示正常,1表示斜体,2表示粗体\r\n` +
    baseId +
    `,` +
    Align +
    `,` +
    X +
    `,` +
    Y +
    `--对齐方式,横纵坐标\r\n` +
    `)` +
    `\r\n` +
    `set_obj_hidden(` +
    labelId +
    `,` +
    hidden +
    `)`
  )
}

//可设置宽高的静态文本显示组件
export function BorderLabelGenerator(
  labelId,
  parentId,
  width,
  height,
  color,
  widthLine,
  opa,
  radius,
  isCover,
  strLabel,
  fontSize,
  fontColor,
  fontType,
  baseId,
  align,
  x,
  y,
  hidden
) {
  if (color == null) {
    color = '0xFFFFFF'
  }
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  baseId = 0
  let str =
    labelId +
    `= create_border_label(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    color.replace('#', '0x') +
    `,` +
    widthLine +
    `,` +
    opa +
    `,` +
    radius +
    `,` +
    isCover +
    `,` +
    `"` +
    strLabel +
    `", ` +
    (Math.round(fontSize / 2) - 5) +
    `,` +
    `--字体大小\r\n` +
    fontColor.replace('#', '0x') +
    `,` +
    `--字体颜色\r\n` +
    fontType +
    `,` +
    `--字体样式,0表示正常,1表示斜体,2表示粗体\r\n` +
    baseId +
    `,` +
    1 +
    `,` +
    x +
    `,` +
    y +
    `--对齐方式,横纵坐标\r\n` +
    `)` +
    `\r\n` +
    `set_label_attr(` +
    labelId +
    `,` +
    width +
    `,` +
    fontType +
    `,` +
    align +
    `)` +
    `set_obj_hidden(` +
    labelId +
    `,` +
    hidden +
    `)`
  // console.log('===================fontSize', fontSize)
  return str
}

//图片生成
export function ImageGenerator(imageId, parentId, pngId, baseId, align, x, y, hidden) {
  baseId = 0
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  return (
    imageId +
    ` = create_png(` +
    parentId +
    `, \r\n` +
    pngId.replace('.png', '').replace('P', '') +
    `,--图片路径\r\n` +
    baseId +
    `,--基准Id\r\n` +
    align +
    `,--对齐方式 左对齐是7，居中 9 ，右对齐 8\r\n` +
    x +
    `,-- X坐标\r\n` +
    y +
    `--- y坐标 \r\n` +
    `)` +
    `\r\nset_obj_hidden(` +
    imageId +
    `,` +
    hidden +
    `)`
  )
}

export function OnControlNotifyGenerator(luaCode) {
  return `function on_control_notify(objId,value,user_data,type) \r\n` + luaCode + `\r\n end`
}

export function OnControlNotifyUiGenerator(luaCode) {
  return `function on_control_notify_ui(objId,type,user_data,value) \r\n` + luaCode + `\r\n end`
}

//
export function OnSystickUiGenerator(luaCode) {
  return `function on_systick_ui() \r\n` + luaCode + `\r\n end`
}
//modbus类型数字显示组件
export function ModbusDataLabelGenerator(
  labelId,
  parentId,
  width,
  height,
  color,
  widthLine,
  opa,
  radius,
  isCover,
  address,
  dotNum,
  isNagetive,
  fontSize,
  fontColor,
  fontType,
  baseId,
  align,
  x,
  y,
  isNeedHideDot,
  hidden
) {
  if (color == null) {
    color = '0xFFFFFF'
  }
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  baseId = 0
  return (
    labelId +
    `= create_data_label(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    color.replace('#', '0x') +
    `,` +
    widthLine +
    `,` +
    opa +
    `,` +
    radius +
    `,` +
    isCover +
    `,` +
    address +
    `,` +
    dotNum +
    `,` +
    isNagetive +
    `,` +
    (Math.round(fontSize / 2) - 5) +
    `,` +
    fontColor.replace('#', '0x') +
    `,` +
    fontType +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `,` +
    isNeedHideDot +
    `)` +
    `\r\n` +
    `set_obj_hidden(` +
    labelId +
    `,` +
    hidden +
    `)`
  )
}

//用户自定义类型数字显示组件
export function UserDataLableGenerator(
  labelId,
  parentId,
  width,
  height,
  color,
  widthLine,
  opa,
  radius,
  isCover,
  userDataId,
  address,
  dotNum,
  isNagetive,
  fontSize,
  fontColor,
  fontType,
  baseId,
  align,
  x,
  y,
  isNeedHideDot,
  hidden
) {
  if (color == null) {
    color = '0xFFFFFF'
  }
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  baseId = 0
  return (
    labelId +
    `= create_user_data_label(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    color.replace('#', '0x') +
    `,` +
    widthLine +
    `,` +
    opa +
    `,` +
    radius +
    `,` +
    isCover +
    `,` +
    userDataId +
    `,` +
    address +
    `,` +
    dotNum +
    `,` +
    isNagetive +
    `,` +
    (Math.round(fontSize / 2) - 5) +
    `,` +
    fontColor.replace('#', '0x') +
    `,` +
    fontType +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `,` +
    isNeedHideDot +
    `)` +
    `\r\n` +
    `set_obj_hidden(` +
    labelId +
    `,` +
    hidden +
    `)`
  )
}

// modbus类型数字输入组件
export function ModbusDataInputGenerator(
  inputId,
  parentId,
  width,
  height,
  color,
  widthLine,
  opa,
  radius,
  isCover,
  title,
  address,
  dotNum,
  isNagetive,
  min,
  max,
  fontSize,
  fontColor,
  fontType,
  baseId,
  align,
  x,
  y,
  isNeedHideDot,
  hidden,
  userId
) {
  if (color == null) {
    color = '0xFFFFFF'
  }
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  if (userId == null || userId == undefined || userId == '') {
    userId = 0
  }
  baseId = 0
  return (
    inputId +
    `= create_data_input(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    color.replace('#', '0x') +
    `,` +
    widthLine +
    `,` +
    opa +
    `,` +
    radius +
    `,` +
    isCover +
    `,` +
    `"` +
    title +
    `"` +
    `,` +
    address +
    `,` +
    dotNum +
    `,` +
    isNagetive +
    `,` +
    min * Math.pow(10, dotNum) +
    `,` +
    max * Math.pow(10, dotNum) +
    `,` +
    (Math.round(fontSize / 2) - 5) +
    `,` +
    fontColor.replace('#', '0x') +
    `,` +
    fontType +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `,` +
    isNeedHideDot +
    ',' +
    userId +
    `)` +
    `\r\n` +
    `set_obj_hidden(` +
    inputId +
    `,` +
    hidden +
    `)`
  )
}
//用户自定义类型数字输入组件
export function UserDataInputGenerator(
  inputId,
  parentId,
  width,
  height,
  color,
  widthLine,
  opa,
  radius,
  isCover,
  title,
  userDataId,
  address,
  dotNum,
  isNagetive,
  min,
  max,
  fontSize,
  fontColor,
  fontType,
  baseId,
  align,
  x,
  y,
  isNeedHideDot,
  hidden,
  userId
) {
  if (color == null) {
    color = '0xFFFFFF'
  }
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  if (userId == null || userId == undefined || userId == '') {
    userId = 0
  }
  baseId = 0
  return (
    inputId +
    `= create_user_data_input(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    color.replace('#', '0x') +
    `,` +
    widthLine +
    `,` +
    opa +
    `,` +
    radius +
    `,` +
    isCover +
    `,` +
    `"` +
    title +
    `"` +
    `,` +
    userDataId +
    `,` +
    address +
    `,` +
    dotNum +
    `,` +
    isNagetive +
    `,` +
    min * Math.pow(10, dotNum) +
    `,` +
    max * Math.pow(10, dotNum) +
    `,` +
    (Math.round(fontSize / 2) - 5) +
    `,` +
    fontColor.replace('#', '0x') +
    `,` +
    fontType +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `,` +
    isNeedHideDot +
    ',' +
    userId +
    `)` +
    `\r\n` +
    `set_obj_hidden(` +
    inputId +
    `,` +
    hidden +
    `)`
  )
}
//modbus文字显示
export function ModbusStringLabelGenerator(
  labelId,
  parentId,
  width,
  height,
  color,
  widthLine,
  opa,
  radius,
  isCover,
  address,
  len,
  highFirst,
  language,
  fontSize,
  fontColor,
  fontType,
  baseId,
  align,
  x,
  y,
  staticTextAlign,
  hidden
) {
  if (color == null) {
    color = '0xFFFFFF'
  }
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  baseId = 0
  return (
    labelId +
    `=create_string_label(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    color.replace('#', '0x') +
    `,` +
    widthLine +
    `,` +
    opa +
    `,` +
    radius +
    `,` +
    isCover +
    `,` +
    address +
    `,` +
    address.toString().length +
    `,` +
    +highFirst +
    `,` +
    language +
    `,` +
    (Math.round(fontSize / 2) - 5) +
    `,` +
    fontColor.replace('#', '0x') +
    `,` +
    fontType +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `)\r\n` +
    `set_label_attr(` +
    labelId +
    `,` +
    width +
    `,` +
    `0,` +
    staticTextAlign +
    `)` +
    `\r\n` +
    `set_obj_hidden(` +
    labelId +
    `,` +
    hidden +
    `)`
  )
}

//用户自定义文字显示
export function UserDataStringLabelGenerator(
  labelId,
  parentId,
  width,
  height,
  color,
  widthLine,
  opa,
  radius,
  isCover,
  userDataId,
  address,
  len,
  highFirst,
  language,
  fontSize,
  fontColor,
  fontType,
  baseId,
  align,
  x,
  y,
  staticTextAlign,
  hidden
  // userId
) {
  if (color == null) {
    color = '0xFFFFFF'
  }
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  // if (userId == null || userId == undefined || userId == '') {
  //   userId = 0
  // }

  baseId = 0
  return (
    labelId +
    `= create_string_user_data_label(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    color.replace('#', '0x') +
    `,` +
    widthLine +
    `,` +
    opa +
    `,` +
    radius +
    `,` +
    isCover +
    `,` +
    userDataId +
    `,` +
    address +
    `,` +
    len +
    `,` +
    highFirst +
    `,` +
    language +
    `,` +
    (Math.round(fontSize / 2) - 5) +
    `,` +
    fontColor.replace('#', '0x') +
    `,` +
    fontType +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    // `,` +
    // needFresh +
    // `,` +
    // userId +
    `)\r\n` +
    `set_label_attr(` +
    labelId +
    `,` +
    width +
    `,` +
    `0,` +
    staticTextAlign +
    `)` +
    `\r\n` +
    `set_obj_hidden(` +
    labelId +
    `,` +
    hidden +
    `)`
  )
}
//modbus文字输入
export function ModbusStringInputGenerator(
  labelId,
  parentId,
  width,
  height,
  color,
  widthLine,
  opa,
  radius,
  isCover,
  title,
  address,
  len,
  highFirst,
  language,
  fontSize,
  fontColor,
  fontType,
  baseId,
  align,
  x,
  y,
  staticTextAlign,
  hidden,
  needFresh,
  userId
) {
  if (color == null) {
    color = '0xFFFFFF'
  }
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  if (needFresh == null || needFresh == undefined || needFresh == '') {
    needFresh = 1
  }
  if (userId == null || userId == undefined || userId == '') {
    userId = 0
  }
  baseId = 0
  return (
    labelId +
    `=create_string_input(` +
    parentId +
    `,` +
    width +
    `,` +
    +height +
    `,` +
    +color.replace('#', '0x') +
    `,` +
    +widthLine +
    `,` +
    +opa +
    `,` +
    +radius +
    `,` +
    +isCover +
    `,` +
    `"` +
    title +
    `"` +
    `,` +
    +address +
    `,` +
    +address.toString().length +
    `,` +
    +highFirst +
    `,` +
    +language +
    `,` +
    +(Math.round(fontSize / 2) - 5) +
    `,` +
    +fontColor.replace('#', '0x') +
    `,` +
    +fontType +
    `,` +
    +baseId +
    `,` +
    +align +
    `,` +
    +x +
    `,` +
    +y +
    `,` +
    +needFresh +
    `,` +
    +userId +
    `)\r\n` +
    `set_label_attr(` +
    labelId +
    `,` +
    width +
    `,` +
    `0,` +
    staticTextAlign +
    `)` +
    `\r\n` +
    `set_obj_hidden(` +
    labelId +
    `,` +
    hidden +
    `)`
  )
}
//用户自定义文字输入
export function UserDataStringInputGenerator(
  labelId,
  parentId,
  width,
  height,
  color,
  widthLine,
  opa,
  radius,
  isCover,
  title,
  userDataId,
  address,
  len,
  highFirst,
  language,
  fontSize,
  fontColor,
  fontType,
  baseId,
  align,
  x,
  y,
  staticTextAlign,
  hidden,
  needFresh,
  userId
) {
  baseId = 0
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  if (needFresh == null || needFresh == undefined || needFresh == '') {
    needFresh = 1 // Default value if not provided
  }
  if (userId == null || userId == undefined || userId == '') {
    userId = 0
  }
  return (
    labelId +
    `=create_string_user_data_input(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    color.replace('#', '0x') +
    `,` +
    widthLine +
    `,` +
    opa +
    `,` +
    radius +
    `,` +
    isCover +
    `,` +
    `"` +
    title +
    `"` +
    `,` +
    userDataId +
    `,` +
    address +
    `,` +
    len +
    `,` +
    highFirst +
    `,` +
    language +
    `,` +
    (Math.round(fontSize / 2) - 5) +
    `,` +
    fontColor.replace('#', '0x') +
    `,` +
    fontType +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `,` +
    needFresh + // Added needFresh parameter
    `,` +
    userId + // Added userId parameter
    `)\r\n` +
    `set_label_attr(` +
    labelId +
    `,` +
    width +
    `,` +
    `0,` +
    staticTextAlign +
    `)` +
    `\r\n` +
    `set_obj_hidden(` +
    labelId +
    `,` +
    hidden +
    `)`
  )
}

//进度条组件生成
export function BarGenerator(
  barId,
  parentId,
  width,
  height,
  minrange,
  maxrange,
  colorSelect,
  colorBack,
  baseId,
  align,
  x,
  y,
  hidden
  // type, // 'none', 'modbus', or 'userData'
  // modbusAddress, // only if type is 'modbus'
  // userDataId, // only if type is 'userData'
  // userDataAddress // only if type is 'userData'
) {
  baseId = 0
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  let barCode =
    barId +
    `=create_bar(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    minrange +
    `,` +
    maxrange +
    `,` +
    colorSelect.replace('#', '0x') +
    `,` +
    colorBack.replace('#', '0x') +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y
  // // 只有当类型是'modbus'且modbusAddress有有效值（非空且非0）时才添加地址映射参数
  // if (
  //   type === 'modbus' &&
  //   modbusAddress !== undefined &&
  //   modbusAddress !== '' &&
  //   modbusAddress != 0
  // ) {
  //   barCode +=
  //     `,` +
  //     modbusAddress + // modbus address
  //     `,0xFFFF` // fixed userId for modbus
  // }
  // // 只有当类型是'userData'且userDataId和userDataAddress不同时为0时才添加地址映射参数
  // else if (type === 'userData' && !(userDataId == 0 && userDataAddress == 0)) {
  //   // 只有两者同时为0才跳过
  //   barCode +=
  //     `,` +
  //     (userDataId || 0) + // 如果userDataId为空或undefined，默认0
  //     `,` +
  //     (userDataAddress || 0) // 如果userDataAddress为空或undefined，默认0
  // }
  // // 对于其他情况（type为'none'或未定义，或者两者同时为0），不添加额外参数

  barCode += `)`

  return barCode + `\r\n` + `set_obj_hidden(` + barId + `,` + hidden + `)`
}

//滑块生成
export function SliderGenerator(
  sliderId,
  parentId,
  width,
  height,
  step,
  minrange,
  maxrange,
  colorSelect,
  colorBack,
  colorKnob,
  user_data,
  baseId,
  align,
  x,
  y,
  pos,
  staticText,
  hidden,
  type, // 'none', 'modbus', or 'userData'
  modbusAddress, // only if type is 'modbus'
  userDataId, // only if type is 'userData'
  userDataAddress // only if type is 'userData'
) {
  baseId = 0
  if (staticText == '') {
    staticText = '0'
  }
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  let sliderCode =
    sliderId +
    `=create_slider(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    step +
    `,` +
    minrange +
    `,--起始进度 \r\n` +
    maxrange +
    `,--最大进度 \r\n` +
    colorSelect.replace('#', '0x') +
    `,--颜色选择 \r\n` +
    colorBack.replace('#', '0x') +
    `,--背景颜色 \r\n` +
    colorKnob.replace('#', '0x') +
    `,--圆圈颜色 \r\n` +
    pos +
    `,--当前值 暂时组态就是0 \r\n` +
    staticText +
    `,--数据更新的标签 \r\n` +
    user_data +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y

  // 只有当类型是'modbus'且modbusAddress有有效值（非空且非0）时才添加地址映射参数
  if (
    type === 'modbus' &&
    modbusAddress !== undefined &&
    modbusAddress !== '' &&
    modbusAddress != 0
  ) {
    sliderCode +=
      `,` +
      modbusAddress + // modbus address
      `,0xFFFF` // fixed userId for modbus
  }
  // 只有当类型是'userData'且userDataId和userDataAddress不同时为0时才添加地址映射参数
  else if (type === 'userData' && !(userDataId == 0 && userDataAddress == 0)) {
    // 只有两者同时为0才跳过
    sliderCode +=
      `,` +
      (userDataId || 0) + // 如果userDataId为空或undefined，默认0
      `,` +
      (userDataAddress || 0) // 如果userDataAddress为空或undefined，默认0
  }
  // 对于其他情况（type为'none'或未定义，或者两者同时为0），不添加额外参数

  sliderCode += `)`

  return sliderCode + `\r\n` + `set_obj_hidden(` + sliderId + `,` + hidden + `)`
}

//二维码生成
export function QrCodeGenerator(
  qrId,
  parentId,
  width,
  strLabel,
  colorBack,
  colorQr,
  baseId,
  align,
  x,
  y,
  hidden
) {
  baseId = 0
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  return (
    qrId +
    `= create_qr_code(` +
    parentId +
    `,` +
    width +
    `,"` +
    strLabel +
    `",` +
    colorQr.replace('#', '0x') +
    `,` +
    colorBack.replace('#', '0x') +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `)` +
    `\r\n` +
    `set_obj_hidden(` +
    qrId +
    `,` +
    hidden +
    `)`
  )
}

//动画（轮播图）生成
//先创建图片,获取图片ID,再创建轮播图
export function GifGenerator(
  objId,
  parentId,
  pngStartId,
  pngSize,
  timer,
  baseId,
  align,
  x,
  y,
  hidden
) {
  baseId = 0
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  let png =
    objId +
    `= create_png(` +
    parentId +
    `,` +
    pngStartId.replace('P', '').replace('.png', '') +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `)`
  let gif =
    `start_png_gif(` +
    objId +
    `,` +
    pngStartId.replace('P', '').replace('.png', '') +
    `,` +
    pngSize +
    `,` +
    timer +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `)`
  return png + `\r\n` + gif + `\r\n` + `set_obj_hidden(` + objId + `,` + hidden + `)`
}

export function Gif2Generator(objId, parentId, gifId, baseId, align, x, y, hidden) {
  baseId = 0
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }

  if (gifId == null || gifId == undefined || gifId == '') {
    gifId = '0'
  }
  let ret =
    objId +
    `= play_gif(` +
    parentId +
    `,` +
    gifId.replace('G', '').replace('.gif', '') +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `)\r\n`

  if (hidden == 1) {
    ret += `set_obj_hidden(` + objId + `,` + hidden + `)`
  }
  console.log(ret)
  return ret
}

//音频生成
export function SoundGenerator(soundId, mode, enablePage, voice, hidden) {
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  return (
    `play_sound(` +
    soundId.replace('.wav', '') +
    `,` +
    mode +
    `,` +
    enablePage +
    `)\r\n` +
    `set_sound_voice(` +
    voice +
    `)`
  )
}

//指示灯生成
export function LedGenerator(
  ledId,
  parentId,
  width,
  height,
  baseId,
  align,
  x,
  y,
  type,
  modbusAddress,
  userDataId,
  userDataAddress,
  bit,
  colorOn,
  colorOff,
  hidden
) {
  baseId = 0
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  let led =
    ledId +
    `= create_led(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `)`
  let condition = ``
  if (type == 'modbus') {
    condition =
      `add_led_condition(` +
      ledId +
      `,` +
      modbusAddress +
      `,--通信地址 \r\n` +
      bit +
      `,--bit位 \r\n` +
      colorOn.replace('#', '0x') +
      `,` +
      colorOff.replace('#', '0x') +
      `--开关状态颜色\r\n` +
      `)`
  } else if (type == 'userData') {
    condition =
      `add_led_condition(` +
      ledId +
      `,` +
      userDataId +
      `,--用户自定义区编号 \r\n` +
      userDataAddress +
      `,--通信地址 \r\n` +
      bit +
      `,--bit位 \r\n` +
      colorOn.replace('#', '0x') +
      `,` +
      colorOff.replace('#', '0x') +
      `--开关状态颜色\r\n` +
      `)`
  }

  return led + `\r\n` + condition + `\r\n` + `set_obj_hidden(` + ledId + `,` + hidden + `)`
}
//下拉框
export function ComboboxGenerator(
  comboboxId,
  parentId,
  width,
  height,
  colorBack,
  radius,
  fontSize,
  fontColor,
  fontType,
  widthLine,
  color,
  downWidth,
  downHeight,
  downColor,
  downLine,
  downLineColor,
  downfontSize,
  downFontColor,
  baseId,
  align,
  x,
  y,
  userId,
  comboboxStrings,
  selectedIndex,
  hidden
) {
  baseId = 0
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  let combobox =
    comboboxId +
    `= create_combobox(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    colorBack.replace('#', '0x') +
    `,` +
    radius +
    `,` +
    (Math.round(fontSize / 2) - 5) +
    `,` +
    fontColor.replace('#', '0x') +
    `,` +
    fontType +
    `,` +
    widthLine +
    `,` +
    color.replace('#', '0x') +
    `,` +
    downWidth +
    `,` +
    downHeight +
    `,` +
    downColor.replace('#', '0x') +
    `,` +
    downLine +
    `,` +
    downLineColor.replace('#', '0x') +
    `,` +
    Math.round(downfontSize / 2) +
    `,` +
    downFontColor.replace('#', '0x') +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `,` +
    userId +
    `)`
  let condition = ''
  for (let option of comboboxStrings) {
    condition =
      condition +
      `\r\n` +
      `add_combobox_string(` +
      comboboxId +
      `,` +
      `"` +
      option.label +
      `"` +
      `)`
  }

  return (
    combobox +
    `\r\n` +
    condition +
    `\r\nset_combobox_index(` +
    comboboxId +
    `,` +
    (selectedIndex - 1) +
    `)` +
    `\r\n` +
    `set_obj_hidden(` +
    comboboxId +
    `,` +
    hidden +
    `)`
  )
}

export function SwitchGenerator(
  switchId,
  parentId,
  width,
  height,
  fontSize,
  colorSelect,
  colorNoSelect,
  colorSelectFont,
  colorNoSelectFont,
  selectName,
  notSelectName,
  user_data,
  baseId,
  align,
  x,
  y,
  colorSwitch,
  hidden
  // type, // 'none', 'modbus', or 'userData'
  // modbusAddress, // only if type is 'modbus'
  // userDataId, // only if type is 'userData'
  // userDataAddress, // only if type is 'userData'
  // bit
) {
  baseId = 0
  if (colorSwitch == undefined || colorSwitch == '') {
    colorSwitch = '#000000'
  }
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }

  let switchCode =
    switchId +
    `= create_switch(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    (Math.round(fontSize / 2) - 5) +
    `,` +
    colorSelect.replace('#', '0x') +
    `,` +
    colorNoSelect.replace('#', '0x') +
    `,` +
    colorSelectFont.replace('#', '0x') +
    `,` +
    colorNoSelectFont.replace('#', '0x') +
    `,` +
    `"` +
    selectName +
    `"` +
    `,` +
    `"` +
    notSelectName +
    `"` +
    `,` +
    user_data +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `,` +
    colorSwitch.replace('#', '0x')

  // // 只有当类型是'modbus'且modbusAddress有有效值时才添加地址映射参数
  // if (type === 'modbus' && modbusAddress !== undefined && modbusAddress !== '') {
  //   switchCode += `,` + '0xFFFF' + `,` + modbusAddress + `,` + bit // modbus address
  // } else if (
  //   type === 'userData' &&
  //   userDataId !== undefined &&
  //   userDataId !== '' &&
  //   userDataAddress !== undefined &&
  //   userDataAddress !== ''
  // ) {
  //   switchCode +=
  //     `,` +
  //     userDataId + // 如果userDataId为空或undefined，默认0
  //     `,` +
  //     userDataAddress +
  //     `,` +
  //     bit
  // }
  // // 对于其他情况（type为'none'或未定义，或者两者同时为0），不添加额外参数

  switchCode += `)`

  return switchCode + `\r\n` + `set_obj_hidden(` + switchId + `,` + hidden + `)`
}

//滚动背景模块
export function ModuleGenerator(
  moduleId,
  parentId,
  width,
  height,
  color,
  colorLine,
  widthLine,
  opa,
  radius,
  isCover,
  scroll,
  baseId,
  align,
  x,
  y,
  hidden,
  scrollWidth,
  scrollHeight
) {
  baseId = 0
  isCover = 0
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  return (
    moduleId +
    `= create_page_back_scroll_ex(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    color.replace('#', '0x') +
    `,` +
    colorLine.replace('#', '0x') +
    `,` +
    widthLine +
    `,` +
    opa +
    `,` +
    radius +
    `,` +
    isCover +
    `,` +
    scroll +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `,` +
    scrollWidth +
    `,` +
    scrollHeight +
    `)` +
    `\r\n` +
    `set_obj_hidden(` +
    moduleId +
    `,` +
    hidden +
    `)`
  )
}

//生成滑屏 1
export function SlidePageGenerator(
  moduleId,
  parentId,
  width,
  height,
  color,
  colorLine,
  widthLine,
  opa,
  radius,
  isCover,
  baseId,
  align,
  x,
  y,
  hidden,
  leftPictId,
  rightPictId,
  buttonWidth,
  buttonHeight,
  contentList,
  currentPageType,
  currentPageId,
  currentPageUserDataId,
  currentPageUserDataAddress
) {
  baseId = 0
  isCover = 0
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }

  if (rightPictId == null || rightPictId == undefined || rightPictId == '') {
    rightPictId = '0'
  }

  if (leftPictId == null || leftPictId == undefined || leftPictId == '') {
    leftPictId = '0'
  }

  // 处理当前页面ID
  let currentPageCode = '0'
  if (currentPageType === 'userData') {
    currentPageCode = `get_user_data(${currentPageUserDataId}, ${currentPageUserDataAddress})`
  } else {
    currentPageCode = Math.max(0, Math.min(contentList.length - 1, currentPageId || 0))
  }

  // Add scroll_switch_page function if using user data
  let switchPageFunction = ''
  if (currentPageType === 'userData') {
    switchPageFunction =
      `function scroll_switch_page(pageNo)\r\n` +
      ` set_user_data(${currentPageUserDataId}, ${currentPageUserDataAddress}, pageNo)\r\n` +
      `end\r\n\r\n`
  }

  let ret =
    switchPageFunction +
    moduleId +
    `= create_page_back(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,` +
    color.replace('#', '0x') +
    `,` +
    colorLine.replace('#', '0x') +
    `,` +
    widthLine +
    `,` +
    opa +
    `,` +
    radius +
    `,` +
    isCover +
    `,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `)\r\n` +
    'ScrollPageId = create_scroll_page( \r\n' +
    moduleId +
    ', \r\n' +
    +(width - buttonWidth * 2) +
    ', --宽度\r\n' +
    contentList.length +
    ', --切换页面数量\r\n' +
    currentPageCode +
    ', --当前页面id \r\n' +
    leftPictId.replace('.png', '').replace('P', '') +
    ', --左边图片id\r\n' +
    rightPictId.replace('.png', '').replace('P', '') +
    ', --右边图片id\r\n' +
    buttonHeight +
    '--按键高度\r\n' +
    ')\r\n'

  // 生成页内容
  let ret2 = `function scroll_init_page(${moduleId}, pageNo) \r\n`

  contentList.forEach((item, index) => {
    ret2 += `if pageNo==${index} then \r\n` + item + 'end\r\n'
  })
  ret2 += 'end\r\n'

  return ret2 + ret
}

export function TableGenerator(
  tableId,
  parentId,
  width,
  height,
  baseId,
  align,
  x,
  y,
  hidden,
  rows,
  cols,
  widthLine,
  colorLine,
  opa,
  tableAlign,
  tableX,
  tableY
) {
  baseId = 0
  if (hidden == null || hidden == undefined || hidden == '') {
    hidden = 0
  }
  let tableCode =
    `scroll_` +
    tableId +
    `= create_page_back_scroll(` +
    parentId +
    `,` +
    width +
    `,` +
    height +
    `,0x000000,0xff0000,0,0,0,0,3,` +
    baseId +
    `,` +
    align +
    `,` +
    x +
    `,` +
    y +
    `)` +
    `\r\n` +
    tableId +
    `= create_grid_cells(` +
    `scroll_` +
    tableId +
    `,` +
    (rows.length + 1) +
    `,` +
    (cols.length + 1) +
    `,50,50,` +
    widthLine +
    `,` +
    colorLine.replace('#', '0x') +
    `,` +
    opa +
    `,` +
    baseId +
    `,` +
    tableAlign +
    `,` +
    tableX +
    `,` +
    tableY +
    `)` +
    `\r\nset_obj_hidden(` +
    `scroll_` +
    tableId +
    `,` +
    hidden +
    `)`
  for (let i = 0; i < rows.length; i++) {
    tableCode =
      tableCode + `\r\ngrid_cells_set_item_height(` + tableId + `,` + i + `,` + rows[i] + `)`
  }
  for (let j = 0; j < cols.length; j++) {
    tableCode =
      tableCode + `\r\ngrid_cells_set_item_width(` + tableId + `,` + j + `,` + cols[j] + `)`
  }

  return tableCode
}
